package com.lvtn.adsonandroid;

import java.io.InputStream;
import java.util.ArrayList;

import java.util.HashMap;
import android.app.Activity;
//import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListAdapter extends BaseAdapter {

	private Activity activity;
	private ArrayList<HashMap<String, String>> data;
	private static LayoutInflater inflater = null;
	public DownloadImage imageLoader;
	private static String URL = "http://10.0.2.2/android/news_upload/";
	public class DownloadImage extends AsyncTask<String, Void, Bitmap> {
		  ImageView bmImage;

		  public DownloadImage(ImageView bmImage) {
		      this.bmImage = bmImage;
		  }

		  protected Bitmap doInBackground(String... urls) {
		      String urldisplay = urls[0];
		      Bitmap mIcon11 = null;
		      try {
		        InputStream in = new java.net.URL(urldisplay).openStream();
		        mIcon11 = BitmapFactory.decodeStream(in);
		      } catch (Exception e) {
		          Log.e("Error", e.getMessage());
		          e.printStackTrace();
		      }
		      return mIcon11;
		  }

		  protected void onPostExecute(Bitmap result) {
		      bmImage.setImageBitmap(result);
		  }
		}
	public ListAdapter(Context a, ArrayList<HashMap<String,String>> d) {
		Log.d("getView","chayyyyyy");
		//activity = a;
		data = d;
		inflater = (LayoutInflater) a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		//imageLoader = new DownloadImage(URL);
	}
	
	public int getCount() {
		return data.size();
	}
	public Object getItem(int position) {
		return position;
	}
	public long getItemId(int position) {
		return position;
	}
	public View getView(int position, View convertView,	ViewGroup parent) {
		Log.d("getView","chayyyyyy");
		View vi = convertView;
		if (convertView == null)
			vi = inflater.inflate(R.layout.item_list, null);
	//	TextView id_rs = (TextView) vi.findViewById(R.id.id_rs); 
		TextView title = (TextView) vi.findViewById(R.id.title);
	//	TextView link_image_rs = (TextView) vi.findViewById(R.id.link_image_rs);
		TextView date = (TextView) vi.findViewById(R.id.date); 
		TextView user_name = (TextView) vi.findViewById(R.id.user_name);
		TextView price = (TextView) vi.findViewById(R.id.price);
		ImageView thumb_image = (ImageView)vi.findViewById(R.id.image); 
		HashMap<String, String> data_item = new	HashMap<String, String>();
		data_item = data.get(position);
		//id_rs.setText(daftar_rs.get(MainActivity.TAG_ID_RS));
		title.setText(data_item.get("title"));
		//link_image_rs.setText(daftar_rs.get(MainActivity.TAG_LINK_IMAGE_RS));
		date.setText(data_item.get("created_at"));
		user_name.setText(data_item.get("user_name"));
		price.setText(data_item.get("price"));
		Log.d("Link Image",data_item.get("link_image"));
		String link_image = URL + data_item.get("link_image");
		//DownloadImage d = new DowloadImage(thumb_image);
		Log.d("link",link_image);
		new DownloadImage(thumb_image).execute(link_image);
		return vi;
	}
	
}